// pages/orderBefore/orderBefore.js
var app = getApp();
var getSign = require('../../utils/getSign/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseUrl: '',
    title:'',
    orderId:0,
    checkbox1:false,
    checkbox2: false,
    goodsVaule:{},
    goodsInfo: {},
    // this.data.goodsInfo.numb,
    Invoice_Id: 0,
    Invoice_Title:'',
    Invoice_Type:"",
    Product_Brief:'',
    Product_Id:0,
    Product_Pic:'',
    Product_Title:'',
    SKU_Txt:[],
    fwxy_Link:'',
    fwxy_Title:'',
    mztk_Link:'',
    mztk_Title:'',
    payMoney:'',
    payNumber:1,
    MemberPhone:'',
    liuyan:''
  },
  //数量加1
  jia: function () {
    let numb = "goodsInfo.numb"
    let num = this.data.goodsInfo.numb
    this.setData({
      [numb]: num + 1
    })
    this.requestPost('009', this.data.goodsInfo.id, this.data.goodsInfo.numb, this.data.goodsInfo.SpecListVaule)
  },
  //数量减1
  jian: function () {
    let numb = "goodsInfo.numb"
    let num = this.data.goodsInfo.numb
    if (num > 1) {
      this.setData({
        [numb]: num - 1
      })
      this.requestPost('009', this.data.goodsInfo.id, this.data.goodsInfo.numb, this.data.goodsInfo.SpecListVaule)
    }
  },
  liuyan: function(e){
    this.setData({
      liuyan: e.detail.value
    })
  },
  phoneinput:function(e){
    this.setData({
      MemberPhone: e.detail.value
    })
  },
  readxieyi1:function(){
    this.setData({
      checkbox1:!this.data.checkbox1
    })
  },
  readxieyi2: function () {
    this.setData({
      checkbox2: !this.data.checkbox2
    })
  },
  goOutWeb: function (event){
    var url = event.target.dataset['url'].replace('?', '$')
    url = url.replace(/=/g, '%')
    url = url.replace(/&/g, '@')
    wx.navigateTo({
      url: '/pages/outweb/outweb?url=' + url,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      goodsInfo: app.goodsInfo,
      baseUrl: app.baseUrl
    })
    // var flag = 'goodsInfo.SpecListVaule'
    // this.setData({
    //   [flag]: '[' + app.goodsInfo.taochan + ',' + app.goodsInfo.nianxian + ']'
    // })

    //检查Session_key是否过期
    wx.checkSession({
      success: (res) => {
        //未过期
        console.log('session_key shop页面未过期')
        //是否有数据
        if (true) {
          //没有数据 请求(apitype, pageIndex, classId, keys)
          this.requestPost('009', this.data.goodsInfo.id, this.data.goodsInfo.numb,this.data.goodsInfo.SpecListVaule)
          console.log(this.data.goodsInfo)
        }
      },
      fail: (res) => {
        //过期
        wx.clearStorage({
          success: () => {
            wx.switchTab({
              url: '/pages/index/index',
            })
          }
        })
      }
    })
  },

  //发送请求
  requestPost: function (apitype, productid, payNumber, SpecItemList) {
    var memberInfo = wx.getStorageSync('memberInfo')
    var postArgs = {
      'Api_Type': apitype,
      'product_id': productid,
      'pay_Number': payNumber,
      'Spec_Item_List': SpecItemList,
      'Member_Id': memberInfo.MemberId,
      'Member_Token': memberInfo.MemberToken
    }
    //获取
    getSign.request('https://wx-app-api.sjune.cn/yuren/API.ashx', 'POST', postArgs, '').then(
      (res) => {
        console.log(res, '订单支付确认页')
        if (res.Status == 2) {
          wx.clearStorage({
            success: () => {
              wx.switchTab({
                url: '/pages/index/index',
              })
            }
          })
        } else if (res.Status == 0) {
          this.setData({
            Invoice_Id: res.Data.Invoice_Id,
            Invoice_Title: res.Data.Invoice_Title,
            title: res.Data.Invoice_Title,
            Invoice_Type: res.Data.Invoice_Type,
            Product_Brief: res.Data.Product_Brief,
            Product_Id: res.Data.Product_Id,
            Product_Pic: res.Data.Product_Pic,
            Product_Title: res.Data.Product_Title,
            SKU_Txt: res.Data.SKU_Txt.split(/[：，]/g),
            fwxy_Link: res.Data.fwxy_Link,
            fwxy_Title: res.Data.fwxy_Title,
            mztk_Link: res.Data.mztk_Link,
            mztk_Title: res.Data.mztk_Title,
            payMoney: res.Data.payMoney,
            payNumber: res.Data.payNumber
          })
          if (res.Data.MemberPhone == null) {
            this.setData({
              MemberPhone: ''
            })
          } else {
            this.setData({
              MemberPhone: res.Data.MemberPhone
            })
          }
        }
        
      },
      (err) => {
        console.log(err.Message, '成功列表失败')
      }
    )
  },

  goTokaipiao:function(){
    wx.navigateTo({
      // url: '/pages/ticketlist/ticketlist?id=' + this.data.Invoice_Id,
      url: '/pages/ticketlist/ticketlist?id=1',
    })
  },
  goTopayed: function(){
    app.invoiceid = 0
    app.goodsInfo = {}
    if (this.data.checkbox1 == true && this.data.checkbox2 == true){
      if (this.data.MemberPhone == '') {
        wx.showToast({
          title: '请输入手机号',
          duration: 2000
        })
      }else{
//订单提交
  var memberInfo = wx.getStorageSync('memberInfo')
  var postArgs = {
    'Api_Type': '010',
    'product_id': this.data.goodsInfo.id,
    'pay_Number': this.data.goodsInfo.numb,
    'Spec_Item_List': this.data.goodsInfo.SpecListVaule,
    'invoice_Id': this.data.Invoice_Id,
    'Purchase_Remarks': this.data.liuyan,
    'Member_Phone': this.data.MemberPhone,
    'Member_Id': memberInfo.MemberId,
    'Member_Token': memberInfo.MemberToken,

  }
  //获取
  getSign.request('https://wx-app-api.sjune.cn/yuren/API.ashx', 'POST', postArgs, '').then(
    (res) => {
      console.log(res, '订单返回信息')
      this.setData({
        orderId: res.Data.orderId
      })
      this.requestPay(res.Data.orderId)
      
    },
    (err) => {
      console.log(err.Message, '订单失败')
    }
  )
     }
}else{
  wx.showToast({
    title: '协议未同意',
    duration:2000
  })
}
    
  },

  //发送请求
  requestPay: function (orderid) {
    var memberInfo = wx.getStorageSync('memberInfo')
    var postArgs = {
      'Api_Type': '011',
      'orderId': orderid,
      'Member_Id': memberInfo.MemberId,
      'Member_Token': memberInfo.MemberToken
    }
    //获取
    getSign.request('https://wx-app-api.sjune.cn/yuren/API.ashx', 'POST', postArgs, '').then(
      (res) => {
        console.log(res, '支付信息') 
        if (res.Status == 0) {
          wx.requestPayment({
            timeStamp: res.Data.timeStamp,
            nonceStr: res.Data.nonceStr,
            package: res.Data.package,
            signType: res.Data.signType,
            paySign: res.Data.paySign,
            success: (res) => {
              console.log(res)
              wx.navigateTo({
                url: '/pages/payed/payed?orderid=' + this.data.orderId,
              })
            },
            fail(res) { }
          })
        }else{
          console.log('支付信息不对')
        }
       
      },
      (err) => {
        console.log(err.Message, '成功列表失败')
      }
    )
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  seteinvoiceid:function(){
    if (app.invoiceid != 0){
      console.log(app.invoiceid,'fapiaoid')
      this.setData({
        Invoice_Id: app.invoiceid,
        title: app.title
      })
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.seteinvoiceid()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})